<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="字典名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入字典名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="type">
        <el-input
            v-model="queryParams.type"
            placeholder="请输入字典类型"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" >
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList" >
      <el-table-column label="字典编号" align="center" prop="id" />
      <el-table-column label="字典名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="字典类型" align="center" >
        <template #default="scope">
          <el-link type="primary" @click="goDictData(scope.row)"> {{scope.row.type}}
            <el-icon style="margin-left: 10px"><View /></el-icon>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link size="small" type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
          <el-button link size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="getDictApi"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" v-loading="loading">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dict">
import {getDict,addDict,upDict,delDict} from "@/api/admin/index.js";
import {useRouter} from "vue-router";
const router=useRouter();
const { proxy } = getCurrentInstance();
const typeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    page:1,
    size:10
  },
  rules: {
    name: [{ required: true, message: "字典名称不能为空", trigger: "blur" }],
    type: [{ required: true, message: "字典类型不能为空", trigger: "blur" }]
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 查询字典列表 */
async function getDictApi() {
  loading.value = true;
  try {
    let {data,total:totalAll}=await getDict(queryParams.value);
    typeList.value = data;
    total.value = totalAll;
  }finally {
    loading.value = false;
  }
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    type: undefined,
    remark: undefined
  };
  proxy.resetForm("ruleForm");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getDictApi();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加字典";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value={...row};
  open.value=true;
  title.value = "修改字典";
}
/** 提交按钮 */
async function submitForm() {
  await proxy.$refs["ruleForm"].validate();
  loading.value=true;
  try {
    !form.value.id&&await addDict(form.value);
    form.value.id&&await upDict(form.value);
    proxy.$modal.msgSuccess(form.value.id? "修改成功" : "新增成功");
    open.value = false;
    getDictApi();
  }finally {
    loading.value=false;
  }
}
/** 删除按钮操作 */
async function handleDelete(row) {
  await proxy.$modal.confirm(`是否删除字典：${row.name}？`)
  await delDict({id:row.id})
  getDictApi();
  proxy.$modal.msgSuccess("删除成功");
}
function goDictData(row){
  router.push({path:'/menus/dict/dictData',query:{id:row.id}})
}

getDictApi();
</script>
